#!/sbin/openrc-run

depend() {
	after alsasound
	after modules
}

checkconfig() {
	if [ ! -e /proc/i8k ] ; then
		eerror "The i8k driver is not installed"
		return 1
	fi
}

start() {
	checkconfig || return 1
	if [ -n "$VOLUME_UP$VOLUME_DOWN$VOLUME_MUTE" ] ; then
		ebegin "Starting i8kbuttons"
		start-stop-daemon --start --quiet --pidfile /var/run/i8kbuttons.pid \
			--make-pidfile --exec /usr/bin/i8kbuttons --background \
			-- ${VOLUME_UP:+-u "${VOLUME_UP}"} \
			   ${VOLUME_DOWN:+-d "$VOLUME_DOWN"} \
			   ${VOLUME_MUTE:+-m "$VOLUME_MUTE"} \
			   ${BUTTON_REPEAT:+-r "$BUTTON_REPEAT"}
		eend $?
	fi
	if [ ! -n "$NOMON" ]; then
		ebegin "Starting i8kmon"
		start-stop-daemon --start -b --quiet  \
			--pidfile /var/run/i8kmon.pid \
			--make-pidfile \
			--exec /usr/bin/i8kmon --background \
			--name "tclsh /usr/bin/i8kmon" \
			-- -d ${NOAUTO:+-na} ${TIMEOUT:+-t $TIMEOUT}
		eend $?
	fi
}

stop() {
	if [ -n "$VOLUME_UP$VOLUME_DOWN$VOLUME_MUTE" ] ; then
		ebegin "Stopping i8kbuttons"
		start-stop-daemon --stop --quiet --pidfile /var/run/i8kbuttons.pid
		eend $?
	fi
	if [ ! -n "$NOMON" ]; then
		ebegin "Stopping i8kmon"
		start-stop-daemon --stop --quiet --pidfile /var/run/i8kmon.pid
		eend $?
	fi
}
